<?php

namespace App\Kuafu\CouponPackage\Controller\Applet;

use App\Kuafu\CouponPackage\Service\CouponPackageService;
use App\Kuafu\Customer\Service\CustomerPlatformService;
use App\Kuafu\Pay\Model\PayStreamModel;
use App\Kuafu\Pay\Service\PayService;
use App\Kuafu\Shop\Service\ShopService;
use Illuminate\Http\Request;
use App\Kuafu\Base\Controller\BaseController;

class CouponPackageController extends BaseController
{
    // 购买券包
    public function buy(Request $request, $couponPackageId)
    {
        $couponPackageId = (int)$couponPackageId;
        $appletToken     = $request->header('APPLET-TOKEN');
        $platformId      = $request->header('platformId');

        // 获取用户信息
        list($userId, $userOpenId) = CustomerPlatformService::getOpenId($request);

        // 预检查购买状态
        list($orderId, $payPrice) = CouponPackageService::doBuy($couponPackageId, $this->shopId, $userId);

        // 调起支付
        $payConfig = PayService::sMiniPay($this->shopId, $orderId, $payPrice, '夸父点餐购买券包', $userOpenId,
            PayStreamModel::SYS_TYPE_RMS_COUPON_PACKAGE);

        $data = [
            'payConfig' => $payConfig,
            'orderId'   => $orderId,
        ];

        return $this->success($data);
    }
}
